namespace OpenComposite.EII.Repository
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Drawing.Design;
	using System.IO;
	using System.Text;
	using System.Windows.Forms;
	using System.Xml;
	using System.Xml.Schema;

	using OpenComposite.EII.Forms;

	using OpenComposite.Base;
	using OpenComposite.Repository.Properties;

	[RepositoryItem(ItemType.WebMethod, "webmethods", "webmethod_id")]
	public class WebMethod : RepositoryItemBaseEx
	{
		#region Initialization

		/// <summary>
		/// Initializes a new instance of the <see cref="WebMethod"/> class.
		/// </summary>
		public WebMethod()
			: base("webmethod_id", "webmethod_nm", "webmethod_desc", "webmethod_ntext", "<New WebMethod>")
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="WebMethod"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public WebMethod(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="WebMethod"/> class.
		/// </summary>
		/// <param name="id">The id.</param>
		public WebMethod(int id)
			: this()
		{
			this.Load(id);
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets or sets the action.
		/// </summary>
		/// <value>The action.</value>
		public string Action
		{
			get { return getString("webmethod_action", ""); }
			set { setValue("Action", "webmethod_action", value); }
		}

		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return new List<ItemType>(); }
		}

		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override Image Image
		{
			get { return Resources.method; }
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get
			{
				return "method";
			}
		}

		/// <summary>
		/// Gets or sets the input action.
		/// </summary>
		/// <value>The input action.</value>
		public string InputAction
		{
			get { return getString("webmethod_inputaction", ""); }
			set { setValue("InputAction", "webmethod_inputaction", value); }
		}

		/// <summary>
		/// Gets or sets the input schema.
		/// </summary>
		/// <value>The input schema.</value>
		[Editor(typeof(CodeEditorUI), typeof(UITypeEditor))]
		public XmlSchema InputSchema
		{
			get { return getSchema("webmethod_inputschema"); }
			set { setSchema("InputSchema", "webmethod_inputschema", value); }
		}

		/// <summary>
		/// Gets the input schema string.
		/// </summary>
		/// <value>The input schema string.</value>
		[Editor(typeof(CodeEditorUI), typeof(UITypeEditor))]
		public string InputSchemaString
		{
			get { return getString("webmethod_inputschema", ""); }
		}

		/// <summary>
		/// Gets or sets the name of the input type.
		/// </summary>
		/// <value>The name of the input type.</value>
		public string InputTypeName
		{
			get { return getString("webmethod_inputtypenm", ""); }
			set { setValue("InputTypeName", "webmethod_inputtypenm", value); }
		}

		/// <summary>
		/// Gets or sets the name of the input variable.
		/// </summary>
		/// <value>The name of the input variable.</value>
		public string InputVariableName
		{
			get { return getString("webmethod_inputvarnm", ""); }
			set { setValue("InputVariableName", "webmethod_inputvarnm", value); }
		}

		/// <summary>
		/// Gets or sets the name of the input wrapper.
		/// </summary>
		/// <value>The name of the input wrapper.</value>
		public string InputWrapperName
		{
			get { return getString("webmethod_inputwrapper", ""); }
			set { setValue("InputWrapperName", "webmethod_inputwrapper", value); }
		}

		/// <summary>
		/// Gets or sets the output action.
		/// </summary>
		/// <value>The output action.</value>
		public string OutputAction
		{
			get { return getString("webmethod_outputaction", ""); }
			set { setValue("OutputAction", "webmethod_outputaction", value); }
		}

		/// <summary>
		/// Gets or sets the output schema.
		/// </summary>
		/// <value>The output schema.</value>
		[Editor(typeof(CodeEditorUI), typeof(UITypeEditor))]
		public XmlSchema OutputSchema
		{
			get { return getSchema("webmethod_outputschema"); }
			set { setSchema("OutputSchema", "webmethod_outputschema", value); }
		}

		/// <summary>
		/// Gets the output schema string.
		/// </summary>
		/// <value>The output schema string.</value>
		[Editor(typeof(CodeEditorUI), typeof(UITypeEditor))]
		public string OutputSchemaString
		{
			get { return getString("webmethod_outputschema", ""); }
		}

		/// <summary>
		/// Gets or sets the name of the output type.
		/// </summary>
		/// <value>The name of the output type.</value>
		public string OutputTypeName
		{
			get { return getString("webmethod_outputtypenm", ""); }
			set { setValue("OutputTypeName", "webmethod_outputtypenm", value); }
		}

		/// <summary>
		/// Gets or sets the name of the output variable.
		/// </summary>
		/// <value>The name of the output variable.</value>
		public string OutputVariableName
		{
			get { return getString("webmethod_outputvarnm", ""); }
			set { setValue("OutputVariableName", "webmethod_outputvarnm", value); }
		}

		/// <summary>
		/// Gets or sets the name of the output wrapper.
		/// </summary>
		/// <value>The name of the output wrapper.</value>
		public string OutputWrapperName
		{
			get { return getString("webmethod_outputwrapper", ""); }
			set { setValue("OutputWrapperName", "webmethod_outputwrapper", value); }
		}

		/// <summary>
		/// Gets or sets the type of the port.
		/// </summary>
		/// <value>The type of the port.</value>
		public string PortType
		{
			get { return getString("webmethod_porttype", ""); }
			set { setValue("PortType", "webmethod_porttype", value); }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.WebMethod; }
		}

		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return Resources.sWebMethod; }
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override Image SelectedImage
		{
			get { return this.Image; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return this.ImageKey; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "webmethods"; }
		}

		/// <summary>
		/// Gets the web service.
		/// </summary>
		/// <value>The web service.</value>
		public WebService WebService
		{
			get
			{
				if ( this.WebServiceID > 0 ) {
					return this.RepositoryService.GetItem<OpenComposite.EII.Repository.WebService>(this.WebServiceID);
				} else {
					return null;
				}
			}
		}

		/// <summary>
		/// Gets or sets the web service ID.
		/// </summary>
		/// <value>The web service ID.</value>
		public int WebServiceID
		{
			get { return getInt("websvc_id", -1); }
			set { setValue("WebServiceID", "websvc_id", value); }
		}

		#endregion Properties

		#region Methods

		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			WebMethod copy = new WebMethod(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}

		//override
		/// <summary>
		/// Deletes this instance.
		/// </summary>
		/// <param name="bAsk">if set to <c>true</c> show a message to ask for deleting.</param>
		/// <returns></returns>
		public override bool Delete(bool bAsk)
		{
			int id = this.ID;
			bool ok = base.Delete(bAsk);
			if ( ok ) {
				Global.Data.RemoveWebMethodOperations(id);
			}
			return ok;
		}

		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}

		// new
		/// <summary>
		/// Gets the logical method.
		/// </summary>
		/// <returns></returns>
		public LogicalWebMethod GetLogicalMethod()
		{
			return Global.Data.GetLogicalWebMethodOfWebMethod(this.ID);
		}

		/// <summary>
		/// Sets the logical method.
		/// </summary>
		/// <param name="logwmeth">The logwmeth.</param>
		public void SetLogicalMethod(LogicalWebMethod logwmeth)
		{
			LogicalWebMethod oldlogwmeth = this.GetLogicalMethod();
			if ( oldlogwmeth != null ) {
				if ( logwmeth != null && oldlogwmeth.ID == logwmeth.ID ) return;

				oldlogwmeth.WebMethodID = -1;
				oldlogwmeth.Save();
			}
			if ( logwmeth != null ) {
				logwmeth.WebMethodID = this.ID;
				logwmeth.Save();
			}
		}

		#endregion Methods

		#endregion Public Members

		#region Protected Members

		#region Methods

		/// <summary>
		/// Sets the default values.
		/// </summary>
		protected override void SetDefaultValues()
		{
			if ( row == null )
				return;

			base.SetDefaultValues();
		}

		#endregion Methods

		#endregion Protected Members
	}
}